package intermediately

import (
	"encoding/json"
	"fmt"
	"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
	"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
	"os"
)

/**
{
  "orderer": "orderer.example.com",
  "peer": "peer0.org1.example.com",
  "org": "Org1",
  "userName": "Admin",
  "channelID": "mychannel"
}
*/
// 连接的来龙去脉
// 每生成一个新的连接需要创建一个
// Context 结构体去保存上下文
type context struct {
	Orderer        string
	Peer           string
	Org            string
	UserName       string
	ChannelID      string
	ConfigProvider core.ConfigProvider
}

func NewContext() *context {
	return &context{}
}

func (context *context) SetContextJson(contextJsonPath string) *context {
	file, _ := os.Open(contextJsonPath)
	defer file.Close()
	decoder := json.NewDecoder(file)
	err := decoder.Decode(context)
	if err != nil {
		fmt.Println("! context.json decode error ! ----->  " + err.Error())
	}
	return context
}

func (context *context) SetConfigYaml(configYamlPath string) *context {
	context.ConfigProvider = config.FromFile(configYamlPath)
	return context
}
